package com.ruoyi.web.controller.project.standard;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.project.standard.Standard;
import com.ruoyi.system.domain.project.standard.StandardTeacher;
import com.ruoyi.system.domain.project.standard.StandardVo;
import com.ruoyi.system.service.project.IStandardService;
import com.ruoyi.system.service.project.IStandardTeacherService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 标准Controller
 * 
 * @author ruoyi
 * @date 2025-08-28
 */
@RestController
@RequestMapping("/project/standard")
public class StandardController extends BaseController
{
    @Autowired
    private IStandardService standardService;
    @Autowired
    private IStandardTeacherService standardTeacherService;

    /**
     * 查询标准列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Standard standard)
    {
        startPage();
        List<Standard> list = standardService.selectStandardList(standard);
        return getDataTable(list);
    }

    /**
     * 导出标准列表
     */
    @Log(title = "标准", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Standard standard,
                       @RequestParam(required = false) String exportFields)
    {
        List<Standard> list = standardService.selectStandardList(standard);
        List<StandardVo> list1 = new java.util.ArrayList<>();
        for(Standard standard1 : list){
            if(standard1.getTeachers()!= null){
                for(StandardTeacher standardTeacher1 : standard1.getTeachers()){
                    StandardVo vo = new StandardVo();
                    vo.setStandardName(standard1.getStandardName());
                    vo.setStandardType(standard1.getStandardType());
                    vo.setStandardCode(standard1.getStandardCode());
                    vo.setStandardDate(standard1.getStandardDate());
                    vo.setUserName(standardTeacher1.getUserName());
                    vo.setUserType(standardTeacher1.getUserType());
                    list1.add( vo);
                }
            }
        }
        ExcelUtil<StandardVo> util = new ExcelUtil<StandardVo>(StandardVo.class);
        // 如果传递了导出字段，则进行处理
        if (StringUtils.isNotBlank(exportFields)) {
            // 将逗号分隔的字段转为数组，调用showColumn指定显示字段
            String[] fields = exportFields.split(",");
            util.showColumn(fields); // 关键：使用ExcelUtil已有的showColumn方法
        }
        util.exportExcel(response, list1, "标准数据");
    }

    /**
     * 获取标准详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(standardService.selectStandardById(id));
    }

    /**
     * 新增标准
     */
    @Log(title = "标准", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Standard standard)
    {
        StandardTeacher ra = new StandardTeacher();
        if(standard.getId()!=null){
            ra.setStandardId(standard.getId());
        }else {
            ra.setStandardId(-1L);
        }
        List<StandardTeacher> list = standardTeacherService.selectStandardTeacherList( ra);
        String userId =standard.getUserId();
        if(!list.isEmpty()){
            for (StandardTeacher standardTeacher1 : list) {
                if(standardTeacher1.getUserId().equals(userId)){
                    return error("此标准制定您已经参与并有名次记录，请勿重复添加");
                }
            }
        }

        return toAjax(standardService.insertStandard(standard));
    }

    /**
     * 修改标准
     */
    @Log(title = "标准", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Standard standard)
    {
        return toAjax(standardService.updateStandard(standard));
    }

    /**
     * 删除标准
     */
    @Log(title = "标准", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(standardService.deleteStandardByIds(ids));
    }

    /**
     * 根据关键字查询
     */
    @GetMapping("/getInfoByKey")
    public AjaxResult getInfoByKey(  String data)
    {
        Standard ra = standardService.getInfoByKey(data);
        return success(ra);
    }
}
